home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 551-575 / disk_556 / scheme2c / scheme-src.lzh / scrt / sceval.sc < prev    next >
Text File  |  1991-10-11  |  5KB  |  136 lines

  1. ;;; This module implements a simple Scheme evaluator.
  2. ;;;
  3. ;;; Initialization of this module will assure that all modules in the
  4. ;;; "standard" library are initialized.
  5.  
  6. ;*              Copyright 1989 Digital Equipment Corporation
  7. ;*                         All Rights Reserved
  8. ;*
  9. ;* Permission to use, copy, and modify this software and its documentation is
  10. ;* hereby granted only under the following terms and conditions.  Both the
  11. ;* above copyright notice and this permission notice must appear in all copies
  12. ;* of the software, derivative works or modified versions, and any portions
  13. ;* thereof, and both notices must appear in supporting documentation.
  14. ;*
  15. ;* Users of this software agree to the terms and conditions set forth herein,
  16. ;* and hereby grant back to Digital a non-exclusive, unrestricted, royalty-free
  17. ;* right and license under any changes, enhancements or extensions made to the
  18. ;* core functions of the software, including but not limited to those affording
  19. ;* compatibility with other hardware or software environments, but excluding
  20. ;* applications which incorporate this software.  Users further agree to use
  21. ;* their best efforts to return to Digital any such changes, enhancements or
  22. ;* extensions that they make and inform Digital of noteworthy uses of this
  23. ;* software.  Correspondence should be provided to Digital at:
  24. ;* 
  25. ;*                       Director of Licensing
  26. ;*                       Western Research Laboratory
  27. ;*                       Digital Equipment Corporation
  28. ;*                       100 Hamilton Avenue
  29. ;*                       Palo Alto, California  94301  
  30. ;* 
  31. ;* This software may be distributed (but not offered for sale or transferred
  32. ;* for compensation) to third parties, provided such third parties agree to
  33. ;* abide by the terms and conditions of this notice.  
  34. ;* 
  35. ;* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  36. ;* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  37. ;* MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
  38. ;* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  39. ;* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  40. ;* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  41. ;* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  42. ;* SOFTWARE.
  43.  
  44. (module sceval
  45.     (top-level EVAL)
  46.     (with scrt1 scrt2 scrt3 scrt4 scrt5 scrt6 scrt7))
  47.  
  48. (include "repdef.sc")
  49.  
  50. (define-c-external (PROCEDURE->ADDRESS tscp) unsigned "sc_procedureaddress")
  51.  
  52. ;;; The Scheme interpreter starts here.
  53.  
  54. (define (EVAL form . env)
  55.     (exec (expand form) (if env (car env) '())))
  56.  
  57. (define (NEW-ENV idl vals env)
  58.     (cond ((null? idl)
  59.        (if vals (error 'NEW-ENV "Too many arguments to function"))
  60.        env)
  61.       ((symbol? idl)
  62.        (cons (cons idl vals) env))
  63.       ((null? vals)
  64.        (error 'NEW-ENV "Too few arguments to function"))
  65.       (else
  66.        (cons (cons (car idl) (car vals))
  67.          (new-env (cdr idl) (cdr vals) env)))))
  68.  
  69. (define (LOOKUP id env)
  70.     (let ((pair (assq id env)))
  71.      (if pair
  72.          (cdr pair)
  73.          (let ((val (top-level-value id)))
  74.           (if (eq? val $_undefined)
  75.               (error id "Top-level symbol is undefined")
  76.               val)))))
  77.  
  78. (define (ASSIGN id val env)
  79.         (let ((pair (assq id env)))
  80.          (if pair (set-cdr! pair val) (set-top-level-value! id val))))
  81.  
  82. (define (EXEC exp env)
  83.     ((lap (x y) (LOOPSTACKTRACE x y)) exp env)
  84.     (cond ((symbol? exp)
  85.        (lookup exp env))
  86.       ((pair? exp)
  87.        (case (car exp)
  88.          ((quote)
  89.           (cadr exp))
  90.          ((lambda)
  91.           (let ((env  env)
  92.             (vars (cadr exp))
  93.             (body (cons 'begin (cddr exp))))
  94.                (lambda vals
  95.                    (if interpreted-tail-call
  96.                    (list body (new-env vars vals env))
  97.                    (exec body (new-env vars vals env))))))
  98.          ((if)
  99.           (if (exec (cadr exp) env)
  100.               (exec (caddr exp) env)
  101.               (exec (cadddr exp) env)))
  102.          ((set!)
  103.           (assign (cadr exp) (exec (caddr exp) env) env))
  104.          ((begin)
  105.           (do ((exps (cdr exp) (cdr exps)))
  106.               ((null? (cdr exps)) (exec (car exps) env))
  107.               (exec (car exps) env)))
  108.          (else
  109.           (let ((function (exec (car exp) env))
  110.             (args (let loop ((x (cdr exp)) (env env))
  111.                    (if (null? x)
  112.                        x
  113.                        (cons (exec (car x) env)
  114.                          (loop (cdr x) env)))))
  115.             (exp-env '()))
  116.                (cond ((not (procedure? function))
  117.                   (error 'exec
  118.                      "Argument value is not a function: ~s"
  119.                      (car exp)))
  120.                  ((eqv? (procedure->address function)
  121.                     (procedure->address interpreted-proc))
  122.                   (set! interpreted-tail-call #t)
  123.                   (set! exp-env (apply function args))
  124.                   (set! interpreted-tail-call #f)
  125.                   (exec (car exp-env) (cadr exp-env)))
  126.                  (else (apply function args)))))))
  127.       ((number? exp) exp)
  128.       ((string? exp) exp)
  129.       ((char? exp) exp)
  130.       ((boolean? exp) exp)
  131.       (else (error 'exec "Argument is not self-evaluating: ~s" exp))))
  132.  
  133. (define INTERPRETED-TAIL-CALL '())
  134.  
  135. (define INTERPRETED-PROC (exec '(lambda (x) x) '()))
  136.